﻿; (function () {
    var page = {
        name: '',
        relativeUrl: '',
        printArr: [],//所有要打印的placerorderno
        resultList: '',//所有的数据
        finishList: [],
        unFinishList: [],
        init: function () {
            page.relativeUrl = $('#hdnRelativeUrl').val();
            page.schedule = new Schedule("OutPatient", page.relativeUrl);
            page.schedule.days = 1;
            page.schedule.getExamRequestScheduleInfoList(page.getOutPatientListCallback);
            pageEvent.init();
        },
        //获取全部门诊预约
        getOutPatientListCallback: function (data) {
            $('#dataIfo').val(JSON.stringify(data));
            page.name = data[0].Name;
            $('.name').text(data[0].Name);
            $('.sex').text(data[0].Sex);
            $('.age').text(data[0].Age + data[0].AgeUnit);
            $('.department').text(data[0].RequestDeptName);
            $('.phone').text(data[0].ContactPhoneNO);
            var resultList = data;
            page.schedule.examRequestList = data;
            if (resultList.length) {
                var resultList = resultList.sort(page.compare("ScheduleStartDate"));
                page.resultList = resultList;
                page.resultListShow(resultList);
            }
            $('.clinic_foot').show()
          
        },
        //按时间大小重新排序
        compare: function (propertyName) {
            return function (object1, object2) {
                var value1 = object1[propertyName];
                var value2 = object2[propertyName];
                var tOrF = page.checkTime(imcis.convertDateFromJson(value1), imcis.convertDateFromJson(value2));
                if (tOrF) {
                    return -1;
                } else if (tOrF == false) {
                    return 1;
                } else {
                    return 0;
                }
            };
        },
        //两个时间比较返回true or false
        checkTime: function (startTime, EndTime) {
            var start = new Date(startTime.replace("-", "/").replace("-", "/"));
            var end = new Date(EndTime.replace("-", "/").replace("-", "/"));
            if (end < start) {
                return false;
            }
            return true;
        },
        resultListShow: function (resultList) {
            var html = "";
            $.each(resultList, function (i, v) {
                var startDate = imcis.convertDateFromJson(resultList[i].ScheduleStartDate);
                var endDate = imcis.convertDateFromJson(resultList[i].ScheduleEndDate);
                var scheduleTime = startDate.substr(11, 5) + "-" + endDate.substr(11, 5);
                var date = startDate.substr(0, 11);
                var left_none = '';
                if (i == (resultList.length - 1)) {
                    left_none = 'left_none';
                } else { left_none = '' }
                //已预约未预约判断
                var btnStr = '';
                var inputCheckStr = "";
                if (resultList[i].ScheduleStatus == 0) {
                    page.finishList.push(resultList[i]);
                    inputCheckStr = '<input type="checkbox" name="check"  class="check checked_bg" />';
                    btnStr = '<input type="hidden" class="change_input" value=""/><div class="btn_edit">修改</div>' +
                             '<div class="btn_cancle">取消</div>';
                } else {
                    inputCheckStr = "";
                    page.unFinishList.push(resultList[i]);
                    btnStr = '<div class="btn_edit">修改</div>';
                }
                html += '<div class="clinic_div ' + left_none + '"   data-placerorderno=' + resultList[i].PlacerOrderNO + '>' +
                        '<div class="clinic_num">' + (i + 1) + '</div>' +
                        '<div class="clinic_main">' +
                            '<div class="clinic_time">' + date + '</div>' +
                            '<div class="clinic_list">' +
                                '<ul>' +
                                    '<li class="clinic_list_check"> ' + inputCheckStr + '</li>' +
                                    '<li class="clinic_list_name">' +
                                        '<span>' + resultList[i].ServiceSectID + ' </span>' +
                                        '<span>' + resultList[i].ProcedureName + ' </span>' +
                                    '</li>' +
                                    '<li class="clinic_list_time">' +
                                       scheduleTime +
                                    '</li>' +
                                    '<li class="clinic_list_add">' + resultList[i].ScheduleResourceName + "-" + resultList[i].ObservationLocation +
                                    '</li>' +
                                    '<li class="clinic_list_btn">' + btnStr +
                                    '</li>' +
                                '</ul>' +
                            '</div>' +
                       ' </div>' +
                   ' </div>'
            });
            $('#outPaitientContainer').html('');
            $('#outPaitientContainer').html(html);
            var searchBar = $('#searchBar').height()+20;
            if (isNaN(searchBar)) {  $('#outPaitientContainer').height($(window).height() - 200)} else { 
                $('#outPaitientContainer').height($(window).height() - 200 - searchBar);
            }
            if (page.finishList.length) {
                $(".print_num").text(page.finishList.length);
                $(".print_num").closest('.btn_print ').removeClass('btn_no_print')
            }
        }
    }
    var pageEvent = {
        init: function () {
            //选中某一条数据
            $(".clinic_container").on('click', '.check', function () {
                var $this = $(this);
                var isChecked = $this.prop('checked');
                if (isChecked) {
                    $this.addClass('checked_bg');
                } else {
                    $this.removeClass('checked_bg')
                }
                pageEvent.printNum();

            });
            //全选取消全选
            $("#mainContain").on('click', '.check_all', function () {
                var $this = $(this);
                var checkArr = $('.clinic_container .check');
                if ($this.text().trim() == '取消全选') {
                    $('.check_all').text('全选');
                    $('.btn_print ').addClass('btn_no_print');
                    checkArr.each(function (i) {
                        checkArr.eq(i).removeClass('checked_bg')
                    })
                } else {
                    $('.check_all').text('取消全选');
                    $('.btn_print ').removeClass('btn_no_print');
                    checkArr.each(function (i) {
                        checkArr.eq(i).addClass('checked_bg')
                    })
                }
                pageEvent.printNum();

            });
            //取消预约
            $('#outPaitientContainer').on('click', '.btn_cancle',  pageEvent.cancelSchedule);
            //打印
            $('.clinic_foot').on('click', '.btn_print', pageEvent.print);
            //修改预约
            $('#outPaitientContainer').on('click', '.btn_edit', pageEvent.edit);
            //确认预约
            $('.clinic_foot').on('click', '.btn_save', pageEvent.saveMultiExamSchedule);
        },
        //取消预约
        cancelSchedule: function () {
            var $this = $(this);
            page.schedule.comfirm('是否取消预约', function () { 
                var placerOrderNO = $this.closest('.clinic_div').data("placerorderno");
                var exam = null;
                $.each(page.resultList, function (i, n) {
                    if (n.PlacerOrderNO == placerOrderNO) {
                        exam = n;
                        return false;
                    }
                });
                page.schedule.cancelSchedule({
                        'PlacerOrderNO': placerOrderNO
                    }, function (data) {
                    page.schedule.getExamRequestScheduleInfoList(page.getOutPatientListCallback);
                 });
            });
        },
        //打印条数
        printNum: function () {
            page.printArr = [];
            var checkArr = $('#outPaitientContainer .checked_bg');
            $.each(checkArr, function (i) {
                var cancle_btn = checkArr.eq(i).closest('.clinic_div').find('.btn_cancle');
                if (cancle_btn.length) {
                    var placerorderno = checkArr.eq(i).closest('.clinic_div').data('placerorderno');
                    page.printArr.push(placerorderno);
                }
            });
            $('.print_num').text(page.printArr.length);
            if (page.printArr.length) {
                $('.btn_print').removeClass('btn_no_print')
            } else {
                $('.btn_print').addClass('btn_no_print')
            }
        },
        //打印
        print: function () {
            var printNum = page.printArr.length;
            $.each(page.printArr, function (i, n) {
                setTimeout(function () {
                    page.schedule.print(n);
                }, 2000);
            });
            if (printNum == 0) {
                page.schedule.alert('请选择要打印的预约!')
                
            }
        },
        //编辑跳页面
        edit: function () {
            $('#outPatientMain').hide();
            $('.patient_main').hide();
            $('.technology_container').show();
            var Placerorderno = $(this).closest('.clinic_div').data('placerorderno');
            $('#chosePlacerorderno').val(Placerorderno);
            $('#mainContain').appendPage('/ScheduleV2/Schedule/GetPartialView?viewName=_TechnologyPartial', ['/Areas/ScheduleV2/Content/css/Technology.css', '/Areas/ScheduleV2/Scripts/schedule/_TechnologyPartial.js'], function () {
               

            })
        },
        //确认预约
        saveMultiExamSchedule: function () {
            page.schedule.saveExamSchedule(null, function (data) {
                page.schedule.getExamRequestScheduleInfoList(page.getOutPatientListCallback);
            });
        }
    }
    page.init();

})()